package cn.edu.zut.internship.web.controller.company;

import cn.edu.zut.internship.bean.sys.Result;
import cn.edu.zut.internship.bean.sys.SysUser;
import cn.edu.zut.internship.iservice.company.CompanySV;
import cn.edu.zut.internship.web.utils.FileNameUtil;
import cn.edu.zut.internship.web.utils.FileUtil;
import cn.edu.zut.internship.web.utils.UserInfoUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

@RestController
@RequestMapping("/company")
public class CompanyController {
    @Reference
    CompanySV companySV;
    @Value("${zn.edu.zut.internship.url}")
    private String localUrl;
    @Value("${zn.edu.zut.internship.localPath}")
    private String localPath;
    /**
     * 实习实训单位列表查询（审核通过）
     *
     * @param params
     * @return
     */
    @RequestMapping("/getCompanyList")
    public Result getCompanyList(@RequestBody Map params) {
        Result result = new Result();
        params.put("internshipStatus","1");
        result = companySV.selectCompany(params);
        return result;
    }

    /**
     * 待审核实习实训单位列表查询
     *
     * @param params
     * @return
     */
    @RequestMapping("/getVerifyCompanyList")
    public Result getVerifyCompanyList(@RequestBody Map params) {
        Result result = new Result();
        SysUser userInfo = UserInfoUtil.getUserInfo();
        params.put("updateAccount",userInfo.getUserAccount());
        result = companySV.getVerifyCompanyList(params);
        return result;
    }
  /**
     * 审核通过该实习单位
     *
     * @param params
     * @return
     */
    @RequestMapping("/passCompny")
    public Result passCompny(@RequestBody Map params) {
        Result result = new Result();
        SysUser userInfo = UserInfoUtil.getUserInfo();
        params.put("userInfo",userInfo);
        result = companySV.passCompny(params);
        return result;
    }

    /**
     * 学生选报此实习实训单位
     * @param params
     * @return
     */
    @RequestMapping("/choiceCompany")
    public Result choiceCompany(@RequestBody Map params) {
        Result result = new Result();
        SysUser userInfo = UserInfoUtil.getUserInfo();
        params.put("creatAccount",userInfo.getUserAccount());
        result = companySV.choiceCompany(params);
        return result;
    }

    /**
     * 实习实训单位详情查询
     *
     * @param params
     * @return
     */
    @RequestMapping("/getCompany")
    public Result getCompany(@RequestBody Map params) {

        Result result = new Result();
//        Long companyId = Long.valueOf((String) params.get("companyId"));
        result = companySV.getCompanyById(params);
        return result;
    }
    /**
     * 我的实习实训单位详情查询
     *
     * @param
     * @return
     */
    @GetMapping("/getMyCompany")
    public Result getMyCompany() {
        Result result = new Result();
        SysUser userInfo = UserInfoUtil.getUserInfo();
        String userAccount = userInfo.getUserAccount();
        result = companySV.getMyCompany(userAccount);
        return result;
    }

    /**
     * 新增实习实训单位
     *
     * @param params
     * @return
     */
    @RequestMapping("/addCompany")
    public Result addCompany(@RequestBody Map params) {
        Result re = new Result();
        SysUser userInfo = UserInfoUtil.getUserInfo();
        String userAccount = userInfo.getUserAccount();
        params.put("creatAccount",userAccount);
        params.put("userInfo",userInfo);
        re = companySV.addCompany(params);
        return re;
    }

    /**
     * 修改实训单位
     * @param params
     * @return
     */
    @RequestMapping("/updateCompany")
    public Result updateCompany(@RequestBody Map params) {
        Result result = new Result();
        result = companySV.updateCompany(params);
        return result;
    }

    /**
     * 修改当前用户选报实训单位信息
     * @param params
     * @return
     */
    @RequestMapping("/editMyCompany")
    public Result editMyCompany(@RequestBody Map params) {
        Result result = new Result();
        result = companySV.editMyCompany(params);
        return result;
    }
    /**
     * 驳回实训单位
     * @param params
     * @return
     */
    @RequestMapping("/rejectCompany")
    public Result rejectCompany(@RequestBody Map params) {
        Result result = new Result();
        SysUser userInfo = UserInfoUtil.getUserInfo();
        String userAccount = userInfo.getUserAccount();
        params.put("updateAccount",userAccount);
        params.put("userInfo",userInfo);
        result = companySV.rejectCompany(params);
        return result;
    }

    /**
     * 删除实习实训单位
     *
     * @param params
     * @return
     */
    @RequestMapping("/deleteCompany")
    public Result deleteCompany(@RequestBody Map params) {
        Result result = new Result();
        Long companyId = Long.valueOf((String)params.get("companyId"));
        result = companySV.delectCompany(companyId);
        return result;
    }
    /**
     * 列表查询当前用户实习实训单位提交历史
     *
     * @param params
     * @return
     */
    @RequestMapping("/addCompanyHistory")
    public Result addCompanyHistory(@RequestBody Map params) {
        Result result = new Result();
        SysUser userInfo = UserInfoUtil.getUserInfo();
        String userAccount = userInfo.getUserAccount();
        params.put("userAccount",userAccount);
        result = companySV.addCompanyHistory(params);
        return result;
    }

    /**
     * 修改实训单位提交申请
     * @param params
     * @return
     */
    @RequestMapping("/editCompany")
    public Result editCompany(@RequestBody Map params) {
        Result result = new Result();
        SysUser userInfo = UserInfoUtil.getUserInfo();
        String userAccount = userInfo.getUserAccount();
        params.put("userAccount",userAccount);
        result = companySV.editCompany(params);
        return result;
    }

    /**
     * 公用上传接口
     * @param file
     * @return
     */
    @RequestMapping("/upload")
    public Result fileUpload( @RequestParam("file") MultipartFile file) {
        Result result = new Result();
        try {
            byte[] bytes = file.getBytes();//获取字节流
            String fileName = file.getOriginalFilename();//获取上传文件名
            fileName = FileNameUtil.getFileName()+"_"+fileName;//随机生成文件名
            String path = localPath;
            FileUtil.fileupload(file.getBytes(), path, fileName);
            result.getBean().put("url",localUrl+fileName);
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(-9999);
            result.setMessage("上传失败");
        }
        return result;
    }
}

